package org.stormphoenix.bbsfamily

import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.test.context.junit4.SpringRunner
import org.stormphoenix.bbsfamily.config.UserDetailsDO
import org.stormphoenix.bbsfamily.config.UserService
import org.stormphoenix.bbsfamily.dao.RoleMapper
import org.stormphoenix.bbsfamily.dao.UserMapper
import org.stormphoenix.bbsfamily.dao.UserRepository
import org.stormphoenix.bbsfamily.entity.UserDO
import java.sql.Timestamp

@RunWith(SpringRunner::class)
@SpringBootTest
internal class MapperTest {
    @Autowired
    lateinit var userRepository: UserRepository

    @Autowired
    lateinit var userMapper: UserMapper

    @Autowired
    lateinit var roleMapper: RoleMapper

    @Autowired
    lateinit var passwordEncoder: PasswordEncoder

    @Test
    fun findRole() {
        var role = roleMapper.findRoleById(2)
        Assert.assertNotNull(role)
        Assert.assertNotNull(role!!.authorities)

        var roles = roleMapper.findRolesByUserId(21)
        Assert.assertNotNull(roles)
    }

    @Test
    fun findUser() {
//        var user = userMapper.findUserByUsername("liwenshu")
//        Assert.assertNotNull(user)

        var user1 = userMapper.findUserByUsername("wangcheng")
        Assert.assertNotNull(user1)

        var authorities = UserDetailsDO(user1!!).authorities
        Assert.assertNotNull(authorities)
    }

    @Test
    fun saveUser() {
        val user = UserDO(username = "xiangsanliu", password = passwordEncoder.encode("fucking kotlin"))
        user.roles = UserService.DEFAULT_ROLES
        user.regTime = Timestamp(System.currentTimeMillis())
        var result = userRepository.save(user)
        Assert.assertNotNull(result)
        userRepository.deleteById(result.id)
    }
}